import sbt._
import Keys._

object Build extends Build {

  import BuildSettings._
  import Dependencies._

  override lazy val settings = super.settings :+ {
    shellPrompt := (s => Project.extract(s).currentProject.id + " > ")
  }

  ///////////////////////////////////////////////////////////////
  // root project
  ///////////////////////////////////////////////////////////////
  lazy val root = Project("root", file("."))
    .aggregate(
    appMe,
    ybjCommon, ybjUtil, ybjPersistence, ybjLog)
    .settings(noPublishing: _*)

  ////////////////////////////////////////////////////////////////
  // yangbajing site
  ////////////////////////////////////////////////////////////////
  lazy val appMe = webProject("app-me", file("app/me"), 38080)
    .dependsOn(ybjPersistence, ybjUtil, ybjCommon)
    .settings(
    description := "羊八井个人站点系统",
    libraryDependencies ++= (
      compile(_apachePoi) ++
        compile(_squeryl) ++
        compile(_postgresql)))

  ///////////////////////////////////////////////////////////////
  // ybj projects
  ///////////////////////////////////////////////////////////////
  lazy val ybjLog = ybjProject("ybj-log", file("core/log"))
    .dependsOn(ybjPersistence, ybjUtil, ybjCommon)
    .settings(
    description := "羊八井平台-日志",
    libraryDependencies ++= (
      compile(_salat) ++
        compile(_casbah) ++
        compile(_postgresql) ++
        compile(_tomcatJdbc) ++
        compile(_squeryl)))

  lazy val ybjPersistence = ybjProject("ybj-persistence", file("core/persistence"))
    .dependsOn(ybjUtil, ybjCommon)
    .settings(
    description := "羊八井平台-持久化",
    libraryDependencies ++= (
      provided(_salat) ++
        provided(_casbah) ++
        provided(_postgresql) ++
        provided(_tomcatJdbc) ++
        provided(_squeryl)))

  lazy val ybjUtil = ybjProject("ybj-util", file("core/util"))
    .dependsOn(ybjCommon)
    .settings(
    description := "羊八井平台-实用工具库",
    libraryDependencies ++= (
      compile(_apachePoi) ++
        compile(_commonsEmail) ++
        compile(_javaxMail) ++
        provided(_servlet30) ++
        compile(_salat) ++
        compile(_casbah) ++
        compile(_json4s) ++
        compile(_liftwebCommon)))

  lazy val ybjCommon = ybjProject("ybj-common", file("core/common"))
    .settings(
    description := "羊八井平台-公共库",
    libraryDependencies ++= (
      compile(_bson) ++
        compile(_jodaTime) ++
        compile(_bouncycastle) ++
        compile(_scalaArm) ++
        compile(_akkaActor) ++
        compile(_slf4j) ++
        compile(_logback)))

  private def webProject(id: String, base: File, iPort: Int): Project = {
    import com.earldouglas.xsbtwebplugin.{WebPlugin, PluginKeys}
    ybjProject(id, base)
      .settings(WebPlugin.webSettings: _*)
      .settings(
      PluginKeys.port in WebPlugin.container.Configuration := iPort,
      unmanagedResourceDirectories in Test <+= (baseDirectory) {
        _ / "src/main/webapp"
      },
      libraryDependencies ++= (
        container(_servlet30) ++
          container(_jetty) ++
          test(_selenium)))
  }

  private def ybjProject(id: String, base: File): Project =
    Project(id, base)
      .settings(basicSettings: _*)
      .settings(
      resolvers ++= Seq(
        "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/",
        "Typesafe Snapshots" at "http://repo.typesafe.com/typesafe/snapshots/",
        "spray repo" at "http://repo.spray.io",
        "spray nightlies repo" at "http://nightlies.spray.io",
        "snapshots" at "http://oss.sonatype.org/content/repositories/snapshots",
        "releases" at "http://oss.sonatype.org/content/repositories/releases"),
      libraryDependencies ++= (
        test(_scalatest)))

}
